using NLog.Web;
using WebMvc.Utils;
using WebMvc.Utils.ConfigurationInfo;

namespace WebMvc
{
    public class Program
    {
        /// <summary>
        /// 程序的入口方法
        /// </summary>
        /// <param name="args"></param>
        public static void Main(string[] args)
        {
            WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

            #region log4net引入

            // 1. nuget引入 log4net 和 Microsoft.Extensions.Logging.Log4Net.AspNetCore
            // 2. 准备log4net配置文件
            // 3. 配置使用log4net
            // builder.Logging.AddLog4Net("ConfigFiles/log4net.config");

            #endregion log4net引入

            #region NLog引入

            // 1. 引入 nuget 包 NLog.Web.AspNetCore
            // 2. 准备配置文件
            // 3. 配置使用NLog
            // 4. 如果要支持NLog写入数据库，需要引入NLog.Database包
            builder.Logging.AddNLogWeb("ConfigFiles/NLog.config");

            #endregion NLog引入

            #region 读取配置文件

            // 1. 通过ConfigurationManager
            ReadConfiguration.Show(builder.Configuration);

            // 2. 通过IConfiguration.Bind
            ReadConfiguration.ShowBind(builder.Configuration);

            // 3. 通过IServiceCollection配置，在Controller中注入IOptionsMonitor<ConnectionStringOptions> 或 IOptions<ConnectionStringOptions>使用
            builder.Services.Configure<ConnectionStringOptions>(builder.Configuration.GetSection("ConnectionStrings"));

            #endregion 读取配置文件

            // 启用HTTP Session支持
            builder.Services.AddSession();

            // Add services to the container.
            builder.Services.AddControllersWithViews();

            WebApplication app = builder.Build();

            // 启用HTTP Session支持
            app.UseSession();

            // Configure the HTTP request pipeline.
            if (!app.Environment.IsDevelopment())
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");

            app.Run();
        }
    }
}